package com.bsj.power.common.config.auth;

import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bsj.power.common.def.entity.menu.Menu;
import com.bsj.power.common.def.entity.menu.RoleMenu;
import com.bsj.power.common.def.entity.role.Role;
import com.bsj.power.common.def.entity.user.User;
import com.bsj.power.common.mapper.*;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinhao
 * @version 1.0
 * @description 权限管控, 自定义权限加载实现类
 * @date 2024/4/18
 */

@Profile({"power-master-system", "power-module-job", "power-module-video", "power-module-pls"})
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private MenuMapper menuMapper;

    /**
     * 返回一个账号所拥有的权限码集合
     *
     * @param loginId   登录的用户ID
     * @param loginType 类型 默认为系统内用户， 1为第三方授权使用
     * @return
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> menuSigns = new ArrayList<>();
        Long userId = Long.parseLong(loginId.toString());
        User user = userMapper.selectById(userId);
        if (user != null) {
            List<Long> roleIds = userRoleMapper.selectUserRoleIdList(user.getUserId());
            List<Role> roles = roleMapper.selectBatchIds(roleIds);
            if (!roles.isEmpty()) {
                roleIds = roles.stream().map(Role::getRoleId).collect(Collectors.toList());
                List<RoleMenu> roleMenus = roleMenuMapper.selectList(Wrappers.<RoleMenu>lambdaQuery().in(RoleMenu::getRoleId, roleIds));
                if (!roleMenus.isEmpty()) {
                    List<Integer> menuIds = roleMenus.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
                    List<Menu> menus = menuMapper.selectList(Wrappers.<Menu>lambdaQuery().in(Menu::getMenuId, menuIds));
                    if (!menus.isEmpty()) {
                        menuSigns = menus.stream().map(Menu::getMenuSign).distinct().collect(Collectors.toList());
                    }
                }
            }
        }
        return menuSigns;
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     *
     * @param loginId
     * @param loginType
     * @return
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> roleList = new ArrayList<>();
        if (!loginType.equals(StpUtil.TYPE)) {
            //外部登录默认全部角色
            List<Role> roles = roleMapper.selectList(Wrappers.lambdaQuery());
            roleList.addAll(roles.stream().map(Role::getRoleSign).collect(Collectors.toList()));
            return roleList.stream().distinct().collect(Collectors.toList());
        }
        Long userId = Long.parseLong(loginId.toString());
        User user = userMapper.selectById(userId);
        if (user != null) {
            List<String> list = userRoleMapper.selectUserRoleSign(userId);
            if (list != null && !list.isEmpty()) {
                roleList.addAll(list);
            }
        }
        return roleList.stream().distinct().collect(Collectors.toList());
    }
}
